package cn.tedu.controller;

import cn.tedu.pojo.Item;
import cn.tedu.service.ItemService;
import cn.tedu.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItem(@PathVariable String orderId) throws InterruptedException {
        log.info("输出");
        //随机延迟代码
        if(Math.random()<0.9){
            int t = new Random().nextInt(5000);
            log.info("执行延迟"+t);
            Thread.sleep(t);
        }
        List<Item> itemList = itemService.findItems(orderId);
        return JsonResult.ok().data(itemList).msg("port:"+port);
    }

    @PostMapping("/decreaseNumber")
    public JsonResult decreaseNumber(@RequestBody List<Item> items){
        itemService.updateNum(items);
        return JsonResult.ok().msg("减少商品成功:");
    }
}
